import { createSlice } from "@reduxjs/toolkit";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { getHomeDiscount, getHomeGoodPrice, getHomeGoodScore, getHomeHotRecomamnd } from "../../services/modules/home";

export const fetchHomeDataAction = createAsyncThunk("fetchData", (payload, { dispatch }) => {
    getHomeGoodPrice().then(res => {
        dispatch(changeGoodPriceInfo(res));
    }).catch(err => {
        console.log(err);
    })
    
    getHomeGoodScore().then(res => {
        dispatch( changeGoodScoreInfo(res));
    })
    
    getHomeDiscount().then(res => {
        dispatch(changeDiscountInfo(res));
    })

    getHomeHotRecomamnd().then(res => {
        dispatch(changeHotRecommentInfo(res));
    })
})

const homeSlice = createSlice({
    name:"home",
    initialState: {
        goodPriceInfo: {},
        goodScoreInfo: {},
        discountInfo: {},
        hotRecommentInfo:{}
    },
    reducers: {
        changeGoodPriceInfo(state, { payload })
        {
            state.goodPriceInfo = payload;
        },
        changeGoodScoreInfo(state, { payload })
        {
            state.goodScoreInfo = payload;
        },
        changeDiscountInfo(state, { payload })
        {
            state.discountInfo = payload;
        },
        changeHotRecommentInfo(state, { payload })
        {
            state.hotRecommentInfo = payload;
        }
    },
})

export default homeSlice.reducer;
export const { changeGoodPriceInfo,
               changeGoodScoreInfo,
               changeDiscountInfo,
               changeHotRecommentInfo
             } = homeSlice.actions;